/**
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.application.CartServiceFacade;
import com.deep.trade.cart.InDtoCart;
import com.deep.trade.cart.InDtoCartDelete;
import com.deep.trade.cart.query.OutDtoCartItem;
import com.deep.trade.common.HttpResonsePayload;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;


@Controller
@RequestMapping(value = "/api/v1/cart")
public class CartController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private CartServiceFacade cartServiceFacade;

    @Autowired
    private AppRuntimeEvn appRuntimeEvn;

    /**
     * 添加sku到购物车
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload addSkuToCart(
            @RequestBody InDtoCart inDtoCart,
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode
    ) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCart);

        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        cartServiceFacade.addSkuToCart(inDtoCart);
        return HttpResonsePayload.happy(true);
    }

    /**
     * 从 购物车 删除一个sku
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.DELETE, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload removeSkuFromCart(
            @RequestBody InDtoCartDelete inDtoCartDelete,
            @RequestParam String token) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCartDelete);

        inDtoCartDelete.setToken(token);

        cartServiceFacade.removeSkuFromCart(inDtoCartDelete);
        return HttpResonsePayload.happy(true);
    }

    /**
     * 修改购物车 sku数量
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.PUT, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload alterQuantity(
            @RequestBody InDtoCart inDtoCart,
            @RequestParam String token) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoCart);
        inDtoCart.setToken(token);

        cartServiceFacade.alterQuantity(inDtoCart);
        return HttpResonsePayload.happy(true);
    }

    /**
     * 查看 购物车 列表
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public HttpResonsePayload cartOfMember(
            @RequestParam String tenantCode,
            @RequestParam String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize) throws Exception {
        logger.info("{}, tenantCode={}, token={}, pageNum={}, pageSize={}", CommonUtils.currentClassAndMethod(), tenantCode, token, pageNum, pageSize);

        PageInfo<OutDtoCartItem> pageInfo = cartServiceFacade.cartOfMember(tenantCode, token, pageNum, pageSize);
        return HttpResonsePayload.happy(pageInfo);
    }
}
